<?php

/**
 * 商品详情
 */

namespace app\shop\common;

class InfoCommon extends \app\base\common\SiteCommon {

    protected $id = 0;
    protected $info = [];
    protected $classInfo = [];
    protected $crumb = [];

    public function __construct() {
        parent::__construct();

        $id = request('get', 'id', 0, 'intval');
        if(empty($id)){
            $this->error404();
        }

        $info = target('shop/Shop')->getInfo($id);
        if(empty($info) || !$info['status']) {
            $this->error404();
        }

        if($info['url']) {
            $this->redirect($info['url']);
        }

        $classInfo = target('shop/ShopClass')->getInfo($info['class_id']);
        if(empty($classInfo)) {
            $this->error404();
        }

        $info = target('shop/Shop')->getInfo($id, $classInfo['model_id']);

        $this->setMeta($info['title'] . ' - ' . $classInfo['name']);

        $crumb = target('shop/ShopClass')->loadCrumbList($info['class_id']);
        $this->setCrumb($crumb);

        $info['images'] = unserialize($info['images']);

        $this->id = $id;
        $this->info = $info;
        $this->classInfo = $classInfo;
        $this->crumb = $crumb;

    }

    protected function index() {

        $id = $this->id;
        $info = $this->info;
        $classInfo = $this->classInfo;
        $crumb = $this->crumb;

        $this->pageInfo['name'] = '商品详情';

        $filter = [];
        if($info['filter_id']) {
            $filter = target('site/SiteFilter')->getFilterContent($info['filter_id'], $info['content_id']);
        }

        $pageParams = [];
        $pageParams['id'] = $id;

        $tpl = 'shop_' . $this->siteConfig['tpl_content'];
        if($classInfo['tpl_content']) {
            $tpl = $classInfo['tpl_content'];
        }
        if($info['tpl']) {
            $tpl = $info['tpl'];
        }

        $parentClassInfo =  array_slice($crumb, -2, 1);
        if(empty($parentClassInfo)){
            $parentClassInfo = $crumb[0];
        }else {
            $parentClassInfo = $parentClassInfo[0];
        }
        $topClassInfo = $crumb[0];
        target('site/SiteContent')->where(['content_id' => $info['content_id']])->setInc('view');

        $proId = request('get', 'pro_id', 0, 'intval');
        $shopData = target('shop/Info', 'service')->getData($id, $proId, $info);

        $proInfo = $shopData['proInfo'];
        $specList = $shopData['specList'];
        $skuList = $shopData['skuList'];
        if(empty($proInfo)) {
            $this->error404();
        }

        $brandInfo = target('shop/ShopBrand')->getInfo($info['brand_id']);

        $faqList = target('shop/ShopFaq')->loadList(['has_id' => $id, 'app' => 'shop'], 10);

        $commentList = target('order/OrderComment')->loadList([
            'app' => 'shop',
            'has_id' => $id
        ], 20);


        $countCommentPositive = target('order/OrderComment')->countList([
            'app' => 'shop',
            'has_id' => $id,
            'level' => 2
        ]);
        $countCommentNeutral = target('order/OrderComment')->countList([
            'app' => 'shop',
            'has_id' => $id,
            'level' => 1
        ]);
        $countCommentNegative = target('order/OrderComment')->countList([
            'app' => 'shop',
            'has_id' => $id,
            'level' => 0
        ]);

        $sumComment = $countCommentPositive + $countCommentNeutral + $countCommentNegative;

        $commentPositiveRate = $sumComment ? round(($countCommentPositive / $sumComment) * 100) : 0;
        $commentNeutralRate = $sumComment ? round(($countCommentNeutral / $sumComment) * 100) : 0;
        $commentNegativeRate = $sumComment ? round(($countCommentNegative / $sumComment) * 100) : 0;

        $followInfo = target('shop/ShopFollow')->getWhereInfo([
            'app' => 'shop',
            'has_id' => $info['shop_id']
        ]);
        if(!empty($followInfo)) {
            $info['follow'] = 1;
        }else {
            $info['follow'] = 0;
        }

        $this->assign('info', $info);
        $this->assign('proInfo', $proInfo);
        $this->assign('brandInfo', $brandInfo);
        $this->assign('specList', $specList);
        $this->assign('faqList', $faqList);
        $this->assign('commentList', $commentList);
        $this->assign('commentRate', [
            'positive' => $commentPositiveRate,
            'neutral' => $commentNeutralRate,
            'negative' => $commentNegativeRate,
        ]);
        $this->assign('commentCount', [
            'positive' => $countCommentPositive,
            'neutral' => $countCommentNeutral,
            'negative' => $countCommentNegative,
        ]);
        $this->assign('skuJson', json_encode($skuList));
        $this->assign('filterInfo', $filter);
        $this->assign('classInfo', $classInfo);
        $this->assign('parentClassInfo', $parentClassInfo);
        $this->assign('topClassInfo', $topClassInfo);
        $this->assign('pageParams', $pageParams);
        $this->siteDisplay($tpl);
    }


}